﻿

using HalconDotNet;
using LeaderCCSLaserUI.Models;
using LeaderCCSLaserUI.Services;
using Prism.Commands;
using Prism.Services.Dialogs;
using System;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
using ViewROI;

namespace LeaderCCSLaserUI.ViewModels.Dialogs
{
    public class VisionAlarmDialogViewModel : DialogViewModel
    {
        #region 变量
        private readonly DrawROI drawROI;
        private readonly IPLCService pLC;
        private HTuple row, column;
        private CancellationTokenSource source0;
        #endregion
        #region 属性
        private HImage cameraIamge0;
        public HImage CameraIamge0
        {
            get { return cameraIamge0; }
            set { SetProperty(ref cameraIamge0, value); }
        }
        private HObject cameraAppendHObject0;
        public HObject CameraAppendHObject0
        {
            get { return cameraAppendHObject0; }
            set { SetProperty(ref cameraAppendHObject0, value); }
        }
        private Tuple<string, object> cameraGCStyle0;
        public Tuple<string, object> CameraGCStyle0
        {
            get { return cameraGCStyle0; }
            set { SetProperty(ref cameraGCStyle0, value); }
        }
        private ObservableCollection<ROI> cameraROIList0 = new ObservableCollection<ROI>();
        public ObservableCollection<ROI> CameraROIList0
        {
            get { return cameraROIList0; }
            set { SetProperty(ref cameraROIList0, value); }
        }
        private string pointName;
        public string PointName
        {
            get { return pointName; }
            set { SetProperty(ref pointName, value); }
        }
        #endregion
        #region 方法绑定
        private DelegateCommand<object> operateCommad;        

        public DelegateCommand<object> OperateCommad =>
            operateCommad ?? (operateCommad = new DelegateCommand<object>(ExecuteOperateCommad));

        void ExecuteOperateCommad(object obj)
        {
            switch (obj.ToString())
            {
                case "确定":
                    {
                        var region = CameraROIList0[0].getRegion();
                        HOperatorSet.RegionFeatures(region, "row", out row);
                        HOperatorSet.RegionFeatures(region, "column", out column);
                        DialogParameters param = new DialogParameters();
                        param.Add("row", row);
                        param.Add("column", column);
                        OnRequestClose(new DialogResult(ButtonResult.Yes, param));
                    }
                    break;
                case "取消":
                    OnRequestClose(new DialogResult(ButtonResult.No));
                    break;
                default:
                    break;
            }
        }
        #endregion
        #region 导航
        public override void OnDialogClosed()
        {
            if (source0 != null)
            {
                source0.Cancel();
            }
        }
        public override void OnDialogOpened(IDialogParameters parameters)
        {
            try
            {
                PointName = parameters.GetValue<string>("PointName");
                var img = parameters.GetValue<HObject>("Image");
                CameraIamge0 = new HImage(img);
                HOperatorSet.GetImageSize(img, out var width, out var height);
                row = height / 2;
                column = width / 2;
                ROIRectangle1 rec = new ROIRectangle1();
                rec.createROI(column.D, row.D);
                rec.ROIColor = "blue";
                CameraROIList0.Add(rec);
                source0 = new CancellationTokenSource();
                CancellationToken token = source0.Token;
                Task.Run(() => Run(token), token);
            }
            catch { }
        }
        #endregion
        #region 构造函数
        public VisionAlarmDialogViewModel(IContainerProvider containerProvider) : base(containerProvider)
        {
            Title = "视觉报警";
            drawROI = containerProvider.Resolve<DrawROI>();
            pLC = containerProvider.Resolve<IPLCService>("PLC");
        }
        #endregion
        #region 功能函数
        private void Run(CancellationToken token)
        {
            while (true)
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }
                try
                {
                    var eMGButton = pLC.XCoils_X0[3] && pLC.XCoils_X0[7];
                    var startButton = pLC.XCoils_X0[0] || pLC.XCoils_X0[4];
                    var resetButton = pLC.XCoils_X0[1] || pLC.XCoils_X0[5];
                    if (!eMGButton || resetButton)
                    {
                        System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                        {
                            OnRequestClose(new DialogResult(ButtonResult.No));
                        }));
                        break;
                    }
                    else if (startButton)
                    {
                        System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                        {
                            var region = CameraROIList0[0].getRegion();
                            HOperatorSet.RegionFeatures(region, "row", out row);
                            HOperatorSet.RegionFeatures(region, "column", out column);
                            DialogParameters param = new DialogParameters();
                            param.Add("row", row);
                            param.Add("column", column);
                            OnRequestClose(new DialogResult(ButtonResult.Yes, param));
                        }));
                        break;
                    }
                }
                catch { }
                System.Threading.Thread.Sleep(200);
            }
        }
        #endregion
    }
}
